import * as maptalks from "maptalks";
import baiduSpatialreference from "./baiduSpatialreference";
import gaodeSpa from "./gaodeSpatialreference";


// ====================================================================================================================================================================
// 设置为 高德 空间参考系
export function setGDSpatialReference(map, ctTotalLayers, gdBase) {
  map.setSpatialReference(gaodeSpa)
  ctTotalLayers.forEach(function (layer) {
    layer.hide()
  })
  map.setBaseLayer(gdBase)
  gdBase.show()
}

// 设置 百度 空间参考系
export function setBDSpatialReference(map, ctTotalLayers, bdBase) {
  let xySystem = map.getProjection().code.toLowerCase();
  if (xySystem !== 'baidu') {
    ctTotalLayers.forEach(function (layer) {
      layer.hide()
    })
    map.setSpatialReference(baiduSpatialreference);
    map.setBaseLayer(bdBase)
    bdBase.show()
  } else {
    // 是 百度 坐标系
    ctTotalLayers.forEach(function (layer) {
      layer.hide()
    })
    map.setBaseLayer(bdBase)
    bdBase.show()
  }
}

// 设置为 wgs84(4326) 空间参考系
export function set4326SpatialReference(map, ctTotalLayers, wgs84Base) {
  map.setSpatialReference({projection: 'EPSG:4326'})
  ctTotalLayers.forEach(function (layer) {
    layer.hide()
  })
  map.setBaseLayer(wgs84Base)
  wgs84Base.show()
}

// ====================================================================================================================================================================
// ====================================================================================================================================================================
// wgs84 base
export function create4326Layer() {
  return new maptalks.TileLayer('epsg4326', {
    tileSystem: [1, -1, -180, 90],
    urlTemplate: 'https://t{s}.tianditu.gov.cn/vec_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=1bb8e08d7e6e835020b54cb52a261c38',
    subdomains: ['1', '2', '3', '4', '5'],
    attribution: '&copy; <a target="_blank" href="http://www.tianditu.cn">Tianditu</a>',
  });
}

//  china 4326
export function createXTMapAndLayer() {
  return new maptalks.WMSTileLayer('wms4326', {
    // renderer: 'canvas',
    'urlTemplate': 'http://82.156.217.213:8080/geoserver/pgsql_EPSG4326/wms',
    'crs': 'EPSG:4326',
    'layers': 'pgsql_EPSG4326:sheng',
    'styles': '',
    'version': '1.1.0',
    'format': 'image/png',
    'transparent': true,
    'uppercase': true,
    'REQUEST': 'GetMap',
    'exceptions': 'application/vnd.ogc.se_inimage'
  })
}

//  WMS get 3857 layer
export function create3857Layer(layerName, layerParams) {
  if (!layerName) {
    layerName = 'wms3857'
  }
  if (!layerParams) {
    layerParams = {
      urlTemplate: 'http://82.156.217.213:8080/geoserver/xt3857/wms',
      // 'crs': 'EPSG:3857',
      'layers': 'xt3857:scene3857',
      'styles': '',
      'version': '1.1.1',
      'format': 'image/png',
      'transparent': true,
      'uppercase': true,
      'REQUEST': 'GetMap',
      'exceptions': 'application/vnd.ogc.se_inimage'
    }
  }
  return new maptalks.WMSTileLayer(layerName, layerParams)
}

// 高德 base
export function createGDBaseLayer() {
  return new maptalks.TileLayer('gdBase', {
    placeholder: function (canvas) {
      const ctx = canvas.getContext("2d");
      ctx.font = "20px 宋体";
      ctx.fillText("国脉畅行", 10, 50);
    },
    subdomains: ['01', '02', '03', '04'],
    urlTemplate: 'http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
    attribution: '<img style="vertical-align: middle;margin-left: -10px;margin-right: 5px;" src=""/><span style="font-size: 12px;line-height: 2;">©2022 高德软件 GS(2021)6375号- 甲测资字11111093</span>'
  });
}

// 百度 base
export function createBDBaseLayer() {

  return new maptalks.TileLayer('bdBase', {
    urlTemplate: 'http://online{s}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl&scaler=1&p=1',
    subdomains: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    attribution: '<span style="margin-left: -10px">© 2022 Baidu - GS(2021)6026号 - 甲测资字11111342 - 京ICP证030173号 </span>'
  });
}

/**
 * 百度 路况 图层
 */
export function createBDTrafficLayer() {
  return new maptalks.TileLayer('bdTrafficLayer', {
    // https://traffic.map.baidu.com/traffic/?qt=vtraffic&z=9&x=102&y=47&udt=20220408&fn=BMap.cbkBMAP_CUSTOM_LAYER_0_default_102_47_9_9
    // http//webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}
    // http://its.map.baidu.com:8002/traffic/TrafficTileService?time=${time_num}&v=016&level={z}&x={x}&y={y}
    urlTemplate: `http://its.map.baidu.com:8002/traffic/TrafficTileService?time=${new Date().getTime()}&v=016&level={z}&x={x}&y={y}`,
    subdomains: ['0', '1', '2', '3'],
  });
}

/**
 * 高德  路况 图层
 */
export function createGDTrafficLayer() {
  return new maptalks.TileLayer('gdTrafficLayer', {
    renderer: 'canvas', // 跨域 。set TileLayer's renderer to canvas
    urlTemplate: `http://tm.amap.com/trafficengine/mapabc/traffictile?v=1.0&t=1&x={x}&y={y}&z={z}&t=${new Date().getTime()}`,
    subdomains: ['1', '2', '3', '4'],
  });
}

/**
 *  百度卫星
 */
export function createBDSatelliteLayer() {
  return new maptalks.TileLayer('bdSatelliteLayer', {
    urlTemplate: `http://shangetu{s}.map.bdimg.com/it/u=x={x};y={y};z={z};v=009;type=sate&fm=46`,
    subdomains: ['0', '1', '2', '3'],
  });
}

/**
 *  高德卫星
 */
export function createGDSatelliteLayer() {
  return new maptalks.TileLayer('gdSatelliteLayer', {
    urlTemplate: `https://wprd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=6&x={x}&y={y}&z={z}`,
    subdomains: ['1', '2', '3', '4'],
  });
}

// ========================================================高德================================================================

export function showGDBase(map, ctTotalLayers, gdBase) {
  //高德坐标系
  setGDSpatialReference(map, ctTotalLayers, gdBase)
}

/**
 * 切换到  高德 路况
 * @param map
 * @param ctTotalLayers
 * @param ctTotalLayersMap
 * @param gdBase
 */
export function showGDTraffic(map, ctTotalLayers, ctTotalLayersMap, gdBase) {
  //高德坐标系
  setGDSpatialReference(map, ctTotalLayers, gdBase)
  //展示图层
  ctTotalLayersMap.get('gdTrafficLayer').show()
}

/**
 * 切换到高德卫星
 * @param map
 * @param ctTotalLayers
 * @param ctTotalLayersMap
 * @param gdBase
 */
export function showGDSatellite(map, ctTotalLayers, ctTotalLayersMap, gdBase) {
  //高德坐标系
  setGDSpatialReference(map, ctTotalLayers, gdBase)
  //展示图层
  ctTotalLayersMap.get('gdSatelliteLayer').show()
}

// =================================================百度==================================================================

export function showBDBase(map, ctTotalLayers, bdBase) {
  setBDSpatialReference(map, ctTotalLayers, bdBase)
}

/**
 * 切换到 百度 路况
 * @param map
 * @param ctTotalLayers
 * @param ctTotalLayersMap
 * @param bdBase
 */
export function showBDTraffic(map, ctTotalLayers, ctTotalLayersMap, bdBase) {
  setBDSpatialReference(map, ctTotalLayers, bdBase)
  //展示图层
  ctTotalLayersMap.get('bdTrafficLayer').show()
}


/**
 * 切换到 百度 卫星
 * @param map
 * @param ctTotalLayers
 * @param ctTotalLayersMap
 * @param bdBase
 */
export function showBDSatellite(map, ctTotalLayers, ctTotalLayersMap, bdBase) {
  setBDSpatialReference(map, ctTotalLayers, bdBase)
  //展示图层
  ctTotalLayersMap.get('bdSatelliteLayer').show()
}
